//
//  CustomButton.m
//  CustomButton
//
//  Created by Summer on 15/12/26.
//  Copyright © 2015年 Summer. All rights reserved.
//

#import "CustomButton.h"

@interface CustomButton()

/** 保存Blcok **/
@property(nonatomic, copy) CustomButtonCallBlock clickCall;

/** 是否选中 **/
@property(nonatomic, assign, getter=isCustomSelected) BOOL customSelected;

/** 关闭图片 **/
@property(nonatomic, strong) NSString * offIamgeName;

/** 开启图片 **/
@property(nonatomic, strong) NSString * onImageName;

@end

@implementation CustomButton

#pragma mark - - 初始化按钮
- (instancetype)initWithFrame:(CGRect)frame OFFImageName:(NSString *)offIamgeName ONImageName:(NSString *)onImageName ClickCall:(CustomButtonCallBlock)clickCall{
    
    if (self = [super initWithFrame:frame]) {
        
        self.clickCall          = clickCall;
        self.offIamgeName       = offIamgeName;
        self.onImageName        = onImageName;
        self.customSelected     = NO;
        self.layer.borderColor  = [UIColor grayColor].CGColor;
        self.layer.borderWidth  = 1.0;
        //self.layer.cornerRadius = frame.size.height * .5;
        [self addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}


#pragma mark - - 点击事件
- (void)clickButton:(CustomButton *)button{
    
    self.customSelected = !self.customSelected;
    if (self.clickCall) {self.clickCall();}
}

#pragma mark - - 属性监听器
- (void)setCustomSelected:(BOOL)customSelected{
    _customSelected = customSelected;
    
    if (customSelected) {
        [self setImage:[UIImage imageNamed:self.onImageName] forState:UIControlStateNormal];
    }
    else {
        [self setImage:[UIImage imageNamed:self.offIamgeName] forState:UIControlStateNormal];
    }
}



@end
